﻿
using System ;
using System.Globalization ;


namespace MskEdit.Sdk
{

    public static class SdkValueParser
    {
        public static string TryParseToString ( string str, string fallbackValue ) {
        
            if ( string.IsNullOrEmpty( str ) )
                return fallbackValue ;
                
            return str ;
        }


        public static bool? TryParseToBool ( string str, bool? fallbackValue ) {
        
            bool value ;
            if ( ! bool.TryParse( str, out value ) )
                return fallbackValue ;
                
            return value ;
        }


        public static short? TryParseToShort ( string str, short? fallbackValue ) {
        
            short value ;
            if ( ! short.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out value ) )
                return fallbackValue ;
                
            return value ;
        }


        public static int? TryParseToInt ( string str, int? fallbackValue ) {
        
            int value ;
            if ( ! int.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out value ) )
                return fallbackValue ;
                
            return value ;
        }


        public static long? TryParseToLong ( string str, long? fallbackValue ) {
        
            long value ;
            if ( ! long.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out value ) )
                return fallbackValue ;
                
            return value ;
        }


        public static float? TryParseToFloat ( string str, float? fallbackValue ) {
        
            float value ;
            if ( ! float.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out value ) )
                return fallbackValue ;
                
            return value ;
        }


        public static double? TryParseToDouble ( string str, double? fallbackValue ) {
        
            double value ;
            if ( ! double.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out value ) )
                return fallbackValue ;
                
            return value ;
        }

    }
}
